import { Reactive } from "vue";

function defaultClone(value: any) {
  if (typeof value !== "object" || value === null) return value;
  return JSON.parse(JSON.stringify(value));
}

export default function useResettableReactive<T extends Record<string, any>>(
  value: T,
): [Reactive<T>, () => void] {
  const initialValue = defaultClone(value);
  const state = reactive<T>(value);
  function reset(): void {
    Object.keys(state).forEach((key) => delete state[key]);
    Object.assign(state, defaultClone(initialValue));
  }
  return [state, reset];
}
